%TITLE "J-MONOPOLY-ASM BY JEROME WONG AND JONATHAN TAN"

	P386N
	IDEAL
	DOSSEG
	MODEL	small
	STACK	1024

;=====================================================================
; Macros and Equates
;---------------------------------------------------------------------
INCLUDE "macros.asm"
;Debugging	=	1
RTC      equ     [es:6ch]



;=====================================================================
	DATASEG
;---------------------------------------------------------------------
; Working Area
;---------------------------------------------------------------------
exitStatus	db	0
outbuff		db	256 dup(?)
two		dw	2
click		dw	0
key		db	0

;---------------------------------------------------------------------         
; Message variables
;---------------------------------------------------------------------{{{          
wel_msg         db      'Welcome to the '			; welcome message
prog_name	db	'j-monopoly-asm',0			; program name
key_msg         db      'Press any key to continue',0		; 2nd line
go_msg		db	"You've collected $50 for Stepping on 'GO'",0

wof_msg         db      '$$$ Monopoly v0.1 $$$',0		; Game Title

confirm_exit	db	'Are you sure you want to quit? (y/n)',0

askname1	db	'Enter Player 1 Name: ',0
askname2	db	'Enter Player 2 Name: ',0

turn_msg	db	'Current Turn:',0
cash_msg	db	'Cash:',0
asset_msg	db	'Assets:',0

goodluck_msg	db	"The stars in the heavens agree with you! You've gained $"
goodluck_cash	db	20 dup(0)
badluck_msg	db	"The programmers decided to rid you of your hard-earned $50",0

plyr1name	db	100 dup(0)
plyr2name	db	100 dup(0)

jail_msg	db	'Jailed!',0
jail_msg2	db	'Jailed! You miss the next turn :-p',0
dice_msg	db	'ROLL DICE',0
buy_msg		db	'BUY!',0
pass_msg	db	'PASS',0
upg_msg		db	'UPGRADE',0
rent_msg	db	"You've been charged a rent of $"
rent_end	db	12 dup(0)
buy_msg_2	db	"You just bought a property for $"
buy_end_2	db	12 dup(0)
upg_msg_2	db	"You just upgraded your property for $"
upg_end_2	db	12 dup(0)

wins_msg	db	" is the winner!",0
tied_msg	db	"Oops, we have a tie!",0

roll_msg	db	"Dice roll: "
roll_value	db	5 dup(0)

f_cash_msg	db	"'s cash: $", 0
f_asset_msg	db	"'s assets: $", 0
f_total_msg	db	"Total value at: $", 0
;}}}


;---------------------------------------------------------------------         
; Mouse Coordinate Helper
;---------------------------------------------------------------------{{{
bar_height	dw	14
bar_coord	dw	032, 048, 064, 080, 096,
		dw	112, 128, 144, 160, 176,
		dw	192, 224, 240, 256, 272,
		dw	288, 304, 320, 336, 352,
		dw	368, 384, 400, 416, 432,
		dw	448, 464, 456
mouse_action	db	28 dup(0)
;}}} 

;---------------------------------------------------------------------         
; Pieces
;---------------------------------------------------------------------{{{  
p_row_start	dw	434,434,434,348,348,348,434,434,434,348,262,176,90,90,90,176,176,176,90,90,90,176,262,348
p_col_start	dw	444,371,298,298,225,152,152,79,6,6,6,6,6,79,152,152,225,298,298,371,444,444,444,444
tmp_col_start	dw	0
tmp_col_end	dw	0
tmp_row_start	dw	0
tmp_row_end	dw	0
b_row_start	dw	445,445,445,359,359,359,445,445,445,359,273,187,101,101,101,187,187,187,101,101,101,187,273,359
;}}}


;---------------------------------------------------------------------
; Space Names
;---------------------------------------------------------------------{{{
space_1		db	'Go',0	;Array of 11 bytes each
space_2		db	'Noland',0
space_3		db	'LionHole',0
space_4		db	'Wuthurn',0
space_5		db	'Ring',0
space_6		db	'Kennit',0
space_7		db	'Grune',0
space_8		db	'Rohan',0
space_9		db	'Smeagol',0
space_10	db	'Gondor',0
space_11	db	'Downs',0
space_12	db	'Legolas',0
space_13	db	'Ring',0
space_14	db	'Spore',0
space_15	db	'Moria',0
space_16	db	'Frodo',0
space_17	db	'Smeagol',0
space_18	db	'Dune',0
space_19	db	'Beorn',0
space_20	db	'Bombadil',0
space_21	db	'Ring',0
space_22	db	'Arwen',0
space_23	db	'Isengard',0
space_24	db	'Mordor',0
;}}}

;---------------------------------------------------------------------          
; Turn setup and dice variables
;---------------------------------------------------------------------{{{
	
	curr_plyr	db	0	; 0 or 1
	curr_cash	dw	00
	curr_assets	dw	00
	curr_jailed	db	0
	plyrA_cash	dw	500
	plyrB_cash	dw	500
	plyrA_assets	dw	0
	plyrB_assets	dw	0
	plyrA_util	db	0
	plyrB_util	db	0
	plyrA_space	db	0
	plyrB_space	db	0
	plyrA_jailed	db	0	;2 when newly set; 1 on next turn; 0 next next turn
	plyrB_jailed	db	0
	dice_roll	db	
;}}}

;---------------------------------------------------------------------          
; Board variables
;---------------------------------------------------------------------{{{  
	
	spaces		db	24		;Spaces that can be visited by dice roll.
	curr_space	db				;Current space id for current player
	curr_type	db
	curr_owner	db
	curr_level	db	
	
	space_type	db	0,1,1,1			;0=go,1=property,2=util,3=jail,4=chance 
			db	4,1,1,1
			db	3,1,1,1
			db	4,1,1,1
			db	3,1,1,1
			db	4,1,1,1
	
	space_owner	db	24 dup (2)	 	;0,1 = player; 2 = not owned
	space_level	db	24 dup (0)		;0 = house, 1 = condo, 2 = hotel
	buy_price	db	0,20,20,30
			db	0,40,40,50
			db	0,60,65,70
			db	0,80,85,90
			db	0,100,105,110
			db	0,120,130,140
	upgrade_price	db	0,10,10,15
			db	0,20,20,25
			db	0,30,33,35
			db	0,40,43,45
			db	0,50,53,55
			db	0,60,65,70
	rent_price	db	0,5,5,6			;Action_util_rent doesn't actually use value here.
			db	0,9,9,10
			db	0,14,15,16
			db	0,19,20,21
			db	0,24,25,26
			db	0,33,34,35
	
;}}}


;=====================================================================
	CODESEG

ASSUME	ds:@data, es:@data

;----- From bintodec.obj

	EXTRN	bintoasc:proc

;----- From strio.obj

	EXTRN	StrWrite:proc, StrRead:proc, StrConcat:proc

;----- From strings.obj

	EXTRN	StrLength:proc




Start:
	mov	ax, @data
	mov	ds, ax
	mov	es, ax

	mov	ah, 0fh			; get current video mode
	int	10h
	push	eax

	mov     ah, 00h			; set resolution to 640 x 480
	mov     al, 12h			; 16 colors
	int     10h

	call	welcome
	call	interface

@@turn:
	call	update_status
	call	plystatus

	cmp	[curr_jailed], 0
	jg	@@10
	call	roll_dice

	call	move_piece

	call	action
@@10:
	call	end_turn

	jmp	@@turn

	call	maincontrol

emergency:
	pop	eax
	mov     ah, 00h                 ; set back to original video mode
	int     10h

        mov     ah, 0ch                 ; clear keyboard buffer
        int     21h
	
	mov	[exitStatus], 32
	mov	ah, 04ch
	mov	al, [exitStatus]
	int	021h

;==========================================================================
;                               Procedures
;==========================================================================
;-------------------------------------------------------------------------
; Update Status
;-------------------------------------------------------------------------{{{
PROC	update_status
	pushad
	; Update status bar depending on which player's turn.
	
	; Determine current player.
	xor	bx,bx
	mov	bl, [curr_plyr]	

	;Update "Current" variables.
	mov	di,bx
	add	di,di
	mov	ax, [plyrA_cash + di]		; cash
	mov	[curr_cash], ax
	mov	ax, [plyrA_assets + di]		; assets
	mov	[curr_assets], ax
	mov	al, [plyrA_space + bx]		; curr_space
	mov	[curr_space], al
	
	;Checks if player is in jail.
	
	mov	al,[plyrA_jailed + bx]
	mov	[curr_jailed],al

	popad
	ret

ENDP	update_status
;}}}

;-------------------------------------------------------------------------
; Main Control Loop
;-------------------------------------------------------------------------{{{
PROC	maincontrol

	mov	[key], 0
	mov	[click], 0
looper:
@@10:					; keyboard input block
	mov	ah, 11			; scans for keyboard entry
	int	21h
	or	al, al			; if no entry
	je	@@20			; jump to mouse
	mov	ah, 7			; reads key value if there is
	int	21h
	mov	[key], al
	cmp	al, 01bh		; if user press escape
	je	confirm			; confirm quit?
	jmp	return

@@20:					; mouse input block
	pushad
	mov	ax, 05h			; reads in mouse value
	mov	bx, 0
	int	33h

	cmp	bx, 0
	je	@@31

	cmp	cx, 520
	jl	@@31
	cmp	cx, 627
	jg	@@31

	xor	ebx, ebx
	mov	ecx, 28
@@21:
	mov	ax, [bar_coord + bx]
	cmp	dx, ax
	jl	@@30
	add	ax, [bar_height]
	cmp	dx, ax
	jg	@@30
	mov	[click], cx
	popad
	jmp	return

@@30:
	add	ebx, 2
	loop	@@21

@@31:
	popad

@@40:
	jmp	looper



confirm:
	mov	ax, OFFSET confirm_exit
	mov	di, ax
	call	outmsg
	mov	ah, 7			; if user confirms quit
	int	21h
	cmp	al, 'y'
	je	Exit
	cmp	al, 'Y'
	je	Exit			; jump to exit
	box     456, 480, 0, 640, black
	jmp	looper

Exit:
	call	exiter

return:
	ret
ENDP	maincontrol
;}}}


;--------------------------------------------------------------------------
; clears registers
;--------------------------------------------------------------------------{{{
PROC	clear_reg

	xor     eax, eax
	xor     ebx, ebx
	xor     ecx, ecx
	xor     edx, edx

	ret

ENDP	clear_reg
;}}}

;--------------------------------------------------------------------------
; welcome message
;--------------------------------------------------------------------------{{{
PROC	welcome
	call	clear_reg

        pushad				; saves previous contents of ax into stack

        cursor  12, 25			; display "Welcome to the Word of Fortune Game"
	message wel_msg
        cursor  14, 29			; display "Press any key to continue"
	message key_msg

wait_for_key:
	mov     ah, 0bh			; wait for any key to be pressed
	int     21h

	cmp     al, 0ffh
	jne     wait_for_key

	call	clrscrn
	
        mov     ah, 0ch			; clear keyboard buffer
        int     21h

	cursor  12, 25
	message	askname1
	mov	ax, OFFSET plyr1name
	mov	di, ax
	mov	cl, 013
	call	StrRead
	
	cursor	15, 25
	message	askname2
	mov	ax, OFFSET plyr2name
	mov	di, ax
	mov	cl, 13
	call	StrRead	

done_wel:
	call	clrscrn
	; clear message area

	popad				; retrieves contents into ax from the stack
	
	ret

ENDP	welcome
;}}}

;--------------------------------------------------------------------------
; draws interface
;--------------------------------------------------------------------------{{{
PROC	interface

	pushad
        call	game_title              ; display title of game
        call	squares                 ; display squares where the alphabets sit
	mov	ax, 01h
	int	33h
	popad
	ret

ENDP	interface
;}}}

;--------------------------------------------------------------------------
; displays title
;--------------------------------------------------------------------------{{{
PROC	game_title

        ctrmsg wof_msg, 0, 39					; displays "Name of the Game"
	
        change_color    00, 50, 0, 640, lightgray, lightcyan	; changes color of titles

	ret

ENDP	game_title
;}}}

;--------------------------------------------------------------------------
; alignment for squares
;--------------------------------------------------------------------------{{{
PROC	squares

	; begin game grid
        hline   25, 1, 220, white
        hline   25, 293, 512, white

	hline	111, 1, 512, white

	hline	197, 1, 74, white
	hline	197, 147, 366, white
	hline	197, 439, 512, white

	hline	283, 1, 74, white
	hline	283, 147, 366, white
	hline	283, 439, 512, white

	hline	369, 1, 512, white

	hline	455, 1, 220, white
	hline	455, 293, 512, white

	vline	25, 455, 1, white
	vline	25, 455, 74, white

	vline	25, 197, 147, white
	vline	283, 455, 147, white
	vline	25, 197, 220, white
	vline	283, 455, 220, white
	vline	25, 197, 293, white
	vline	283, 455, 293, white
	vline	25, 197, 366, white
	vline	283, 455, 366, white

	vline	25, 455, 439, white
	vline	25, 455, 512, white

	; begin game heading color
	box	370, 393, 367, 439, lightblue
	box	370, 393, 294, 366, lightblue
	box	284, 307, 294, 366, lightblue
	box	284, 307, 148, 220, magenta
	box	370, 393, 148, 220, magenta
	box	370, 393, 75, 147, magenta
	box	284, 307, 2, 74, red
	box	198, 220, 2, 74, red
	box	112, 135, 2, 74, red
	box	26, 49, 75, 147, brown
	box	26, 49, 148, 220, brown
	box	112, 135, 148, 220, brown
	box	112, 135, 294, 366, green
	box	26, 49, 294, 366, green
	box	26, 49, 367, 439, green
	box	112, 135, 440, 512, blue
	box	198, 220, 440, 512, blue
	box	284, 307, 440, 512, blue

	; begin action window
	hline	29, 518, 639, yellow
	hline	451, 518, 639, yellow
	vline	29, 451, 518, yellow
	vline	29, 451, 639, yellow

	; begin town names
	ctrmsg space_1, 25, 60
	
	ctrmsg space_2, 25, 50
	mov	ax, OFFSET outbuff
	mov	di, ax
	xor	eax, eax
	xor	ecx, ecx
	mov	al, [buy_price+1]
	call	BinToAsc
	ctrmsg outbuff, 26,50

	ctrmsg space_3, 25, 41
	mov	ax, OFFSET outbuff
	mov	di, ax
	xor	eax, eax
	xor	ecx, ecx
	mov	al, [buy_price+2]
	call	BinToAsc
	ctrmsg outbuff, 26,41
	
	ctrmsg space_4, 20, 41
	mov	ax, OFFSET outbuff
	mov	di, ax
	xor	eax, eax
	xor	ecx, ecx
	mov	al, [buy_price+3]
	call	BinToAsc
	ctrmsg outbuff, 21,41
	
	ctrmsg space_5, 20, 32
	mov	ax, OFFSET outbuff
	mov	di, ax
	xor	eax, eax
	xor	ecx, ecx
	mov	al, [buy_price+4]
	call	BinToAsc
	ctrmsg outbuff, 21,32
	
	ctrmsg space_6, 20, 23
	mov	ax, OFFSET outbuff
	mov	di, ax
	xor	eax, eax
	xor	ecx, ecx
	mov	al, [buy_price+5]
	call	BinToAsc
	ctrmsg outbuff, 21,23
	
	ctrmsg space_7, 25, 23
	mov	ax, OFFSET outbuff
	mov	di, ax
	xor	eax, eax
	xor	ecx, ecx
	mov	al, [buy_price+6]
	call	BinToAsc
	ctrmsg outbuff, 26,23
	
	ctrmsg space_8, 25, 14
	mov	ax, OFFSET outbuff
	mov	di, ax
	xor	eax, eax
	xor	ecx, ecx
	mov	al, [buy_price+7]
	call	BinToAsc
	ctrmsg outbuff, 26,14
	
	ctrmsg space_9, 25, 5
	mov	ax, OFFSET outbuff
	mov	di, ax
	xor	eax, eax
	xor	ecx, ecx
	mov	al, [buy_price+8]
	call	BinToAsc
	ctrmsg outbuff, 26,5
	
	ctrmsg space_10, 20, 5
	mov	ax, OFFSET outbuff
	mov	di, ax
	xor	eax, eax
	xor	ecx, ecx
	mov	al, [buy_price+9]
	call	BinToAsc
	ctrmsg outbuff, 21,5
	
	ctrmsg space_11, 14, 5
	mov	ax, OFFSET outbuff
	mov	di, ax
	xor	eax, eax
	xor	ecx, ecx
	mov	al, [buy_price+10]
	call	BinToAsc
	ctrmsg outbuff, 15,5
	
	ctrmsg space_12, 9, 5
	mov	ax, OFFSET outbuff
	mov	di, ax
	xor	eax, eax
	xor	ecx, ecx
	mov	al, [buy_price+11]
	call	BinToAsc
	ctrmsg outbuff, 10,5
	
	ctrmsg space_13, 4, 5
	mov	ax, OFFSET outbuff
	mov	di, ax
	xor	eax, eax
	xor	ecx, ecx
	mov	al, [buy_price+12]
	call	BinToAsc
	ctrmsg outbuff, 5,5
	
	ctrmsg space_14, 4, 14
	mov	ax, OFFSET outbuff
	mov	di, ax
	xor	eax, eax
	xor	ecx, ecx
	mov	al, [buy_price+13]
	call	BinToAsc
	ctrmsg outbuff, 5,14
	
	ctrmsg space_15, 4, 23
	mov	ax, OFFSET outbuff
	mov	di, ax
	xor	eax, eax
	xor	ecx, ecx
	mov	al, [buy_price+14]
	call	BinToAsc
	ctrmsg outbuff, 5,23
	
	ctrmsg space_16, 9, 23
	mov	ax, OFFSET outbuff
	mov	di, ax
	xor	eax, eax
	xor	ecx, ecx
	mov	al, [buy_price+15]
	call	BinToAsc
	ctrmsg outbuff, 10,23
	
	ctrmsg space_17, 9, 32
	mov	ax, OFFSET outbuff
	mov	di, ax
	xor	eax, eax
	xor	ecx, ecx
	mov	al, [buy_price+16]
	call	BinToAsc
	ctrmsg outbuff, 10,32
	
	ctrmsg space_18, 9, 41
	mov	ax, OFFSET outbuff
	mov	di, ax
	xor	eax, eax
	xor	ecx, ecx
	mov	al, [buy_price+17]
	call	BinToAsc
	ctrmsg outbuff, 10,41
	
	ctrmsg space_19, 4, 41
	mov	ax, OFFSET outbuff
	mov	di, ax
	xor	eax, eax
	xor	ecx, ecx
	mov	al, [buy_price+18]
	call	BinToAsc
	ctrmsg outbuff, 5,41
	
	ctrmsg space_20, 4, 50
	mov	ax, OFFSET outbuff
	mov	di, ax
	xor	eax, eax
	xor	ecx, ecx
	mov	al, [buy_price+19]
	call	BinToAsc
	ctrmsg outbuff, 5,50
	
	ctrmsg space_21, 4, 60
	mov	ax, OFFSET outbuff
	mov	di, ax
	xor	eax, eax
	xor	ecx, ecx
	mov	al, [buy_price+20]
	call	BinToAsc
	ctrmsg outbuff, 5,60
	
	ctrmsg space_22, 9, 60
	mov	ax, OFFSET outbuff
	mov	di, ax
	xor	eax, eax
	xor	ecx, ecx
	mov	al, [buy_price+21]
	call	BinToAsc
	ctrmsg outbuff, 10,60
	
	ctrmsg space_23, 14, 60
	mov	ax, OFFSET outbuff
	mov	di, ax
	xor	eax, eax
	xor	ecx, ecx
	mov	al, [buy_price+22]
	call	BinToAsc
	ctrmsg outbuff, 15,60
	
	ctrmsg space_24, 20, 60
	mov	ax, OFFSET outbuff
	mov	di, ax
	xor	eax, eax
	xor	ecx, ecx
	mov	al, [buy_price+23]
	call	BinToAsc
	ctrmsg outbuff, 21,60
	


	ctrmsg prog_name,15, 32

        ret

ENDP	squares
;}}}

;--------------------------------------------------------------------------
; Clears Screen (FAST!)
;--------------------------------------------------------------------------{{{
PROC	clrscrn

        pushad

	mov     ah, 00h			; set resolution to 640 x 480
	mov     al, 12h			; 16 colors
	int     10h

	popad
	ret

ENDP	clrscrn
;}}}

;--------------------------------------------------------------------------
; Update Game Play Status
;--------------------------------------------------------------------------{{{
PROC	plystatus

	pushad

	box	30, 176, 520, 637, black
	box	304, 384, 520, 637, black
	cursor	2, 66
	message	turn_msg
	cursor	3, 66
	cmp	[curr_plyr], 0
	je	@@10
	message	plyr2name
	change_color	46, 64, 520, 637, lightgray, lightmagenta
	jmp	@@20
@@10:
	message	plyr1name
	change_color	46, 64, 520, 637, lightgray, lightcyan
@@20:

	cursor	6, 66
	message	cash_msg
	cursor	7, 66
	mov	ax, OFFSET outbuff
	mov	di, ax
	xor	eax, eax
	mov	ax, [curr_cash]
	mov	ecx, 1
	call	BinToAsc
	call	StrWrite
	cursor	9, 66
	message	asset_msg
	cursor	10, 66
	mov	ax, OFFSET outbuff
	mov	di, ax
	xor	eax, eax
	mov	ax, [curr_assets]
	mov	ecx, 1
	call	BinToAsc
	call	StrWrite

	xor	bx, bx
	mov	bl, [plyrA_space]
	add	bx, bx
	mov	ax, [p_row_start + bx]
	mov	[tmp_row_start], ax
	add	ax, 9
	mov	[tmp_row_end], ax
	mov	ax, [p_col_start + bx]
	mov	[tmp_col_start], ax
	add	ax, 9
	mov	[tmp_col_end], ax
	box	<[tmp_row_start]>, <[tmp_row_end]>, <[tmp_col_start]>, <[tmp_col_end]>, lightcyan

	xor	bx, bx
	mov	bl, [plyrB_space]
	add	bx, bx
	mov	ax, [p_row_start + bx]
	mov	[tmp_row_start], ax
	add	ax, 9
	mov	[tmp_row_end], ax
	mov	ax, [p_col_start + bx]
	add	ax, 10
	mov	[tmp_col_start], ax
	add	ax, 9
	mov	[tmp_col_end], ax
	box	<[tmp_row_start]>, <[tmp_row_end]>, <[tmp_col_start]>, <[tmp_col_end]>, lightmagenta

	cmp	[curr_jailed], 0
	je	@@30
	cursor	4, 69
	message	jail_msg
	change_color	65, 78, 520, 637, lightgray, lightred

@@30:
	popad
	ret

ENDP	plystatus
;}}}

;--------------------------------------------------------------------------
; User presses key to roll dice. Saves random number from 1-6  in dice_roll.
;--------------------------------------------------------------------------{{{
PROC	roll_dice
	pushad

	box	352, 367, 519, 638, black
	cursor	22, 68
	message	dice_msg
	change_color	352, 366, 520, 637, black, blue

@@no:
	call	maincontrol
	cmp	[click], 6
	je	@@yes
	jmp	@@no
@@yes:
	box	352, 367, 519, 638, black
	call 	rand		;Random number stored in ax.
	
	mov	[dice_roll], al	

	mov	ax, OFFSET roll_value
	mov	di, ax
	xor	eax, eax
	mov	al, [dice_roll]
	call	BinToAsc
	cursor	13, 66
	message	roll_msg

	popad
	ret

ENDP	roll_dice
;}}}


;--------------------------------------------------------------------------
; Updates Pieces
;--------------------------------------------------------------------------{{{
PROC	move_piece
	pushad

	xor	ax, ax
	mov	al, [curr_space]		; Add dice roll to curr_space and correct overshoot.
	add	al, [dice_roll]
	cmp	al, [spaces]
	jb	@@10
	sub	al, [spaces]
@@10:
	mov	[curr_space], al
	xor	ax, ax
	mov	al, [curr_plyr]
	mov	di, ax

	xor	bx, bx
	mov	bl, [plyrA_space + di]
	add	bx, bx
	mov	dx, 10
	mul	dx
	mov	dx, ax
	mov	ax, [p_row_start + bx]
	mov	[tmp_row_start], ax
	add	ax, 9
	mov	[tmp_row_end], ax
	mov	ax, [p_col_start + bx]
	add	ax, dx
	mov	[tmp_col_start], ax
	add	ax, 9
	mov	[tmp_col_end], ax
	box	<[tmp_row_start]>, <[tmp_row_end]>, <[tmp_col_start]>, <[tmp_col_end]>, black

	mov	al, [curr_space]
	mov	[plyrA_space + di], al

	xor	bx, bx
	mov	bl, [curr_space]
	add	bx, bx
	mov	ax, [p_row_start + bx]
	mov	[tmp_row_start], ax
	add	ax, 9
	mov	[tmp_row_end], ax
	mov	ax, [p_col_start + bx]
	add	ax, dx
	mov	[tmp_col_start], ax
	add	ax, 9
	mov	[tmp_col_end], ax

	cmp	[curr_plyr],1
	je	@@20
	box	<[tmp_row_start]>, <[tmp_row_end]>, <[tmp_col_start]>, <[tmp_col_end]>, lightcyan
	jmp	@@30
@@20:
	box	<[tmp_row_start]>, <[tmp_row_end]>, <[tmp_col_start]>, <[tmp_col_end]>, lightmagenta

@@30:

	popad

	ret
ENDP	move_piece
;}}}


;--------------------------------------------------------------------------
; Action Procedure
;--------------------------------------------------------------------------{{{
PROC	action
	
	xor	bx, bx
	xor	ax, ax
	mov	bl, [curr_space]
	mov	al, [space_type + bl]
	
	mov	[curr_type], al	;Updates memory
	
	;go to action based on space type.
	cmp	[curr_type], 0
	jne	@@act_1
	call	action_go
	
@@act_1:
	cmp	[curr_type], 1
	jne	@@act_2
	call	action_property
	jmp	@@act_5
@@act_2:
	cmp	[curr_type], 2
	jne	@@act_3
;	call	action_util
	jmp	@@act_5
@@act_3:
	cmp	[curr_type], 3
	jne	@@act_4
	call	action_jail
	jmp	@@act_5
@@act_4:
	cmp	[curr_type], 4
	jne	@@act_5
	call	action_chance
	jmp	@@act_5

@@act_5:
	call	update_status
	call	plystatus
	box	352, 366, 519, 638, black
	cursor	22, 70
	message	pass_msg
	change_color	352, 366, 520, 637, black, blue
@@20:
	call	maincontrol
	cmp	[click], 6
	je	@@pass
	jmp	@@20
@@pass:
	box	352, 366, 519, 638, black

	ret

ENDP	action
;}}}


;--------------------------------------------------------------------------
; Collect money on hitting GO(ld).
;--------------------------------------------------------------------------{{{
PROC	action_go

	mov	ax, OFFSET go_msg
	mov	di, ax
	call	outmsg
 
	mov	bx, 50
	call	add_cash

	ret

ENDP	action_go
;}}}


;--------------------------------------------------------------------------
;Increase cash. Amount of cash to increase stored in bx.
;--------------------------------------------------------------------------{{{
PROC	add_cash
	
	pushad
	
	mov	ax, [plyrA_cash]	;Move current player's cash into ax.
	cmp	[curr_plyr], 0
	je	@@a_c_1
	mov	ax, [plyrB_cash]
@@a_c_1:	
	
	add	ax,bx
	jnc	@@a_c_notcarry
	mov	ax, 0ffffh
					;At this point, ax contains new cash amount.
@@a_c_notcarry:	
	cmp	[curr_plyr], 0		;Update current player's cash.
	jne	@@a_c_2
	mov	[plyrA_cash], ax
	jmp	@@a_c_3
@@a_c_2:	
	mov	[plyrB_cash], ax

@@a_c_3:
	popad	
	ret

ENDP	add_cash
;}}}

;--------------------------------------------------------------------------
;Decrease cash. Amount of cash to decrease stored in bx.
;--------------------------------------------------------------------------{{{
PROC	dec_cash
	
	pushad
	
	mov	ax, [plyrA_cash]	;Move current player's cash into ax.
	cmp	[curr_plyr], 0
	je	@@d_c_1
	mov	ax, [plyrB_cash]	
@@d_c_1:	
	
	sub	ax, bx
	jnc	@@d_c_notcarry
	mov	ax,0000h
					;At this point, ax contains new cash amount.
@@d_c_notcarry:	
	cmp	[curr_plyr],0		;Update current player's cash.
	jne	@@d_c_2
	mov	[plyrA_cash], ax
	jmp	@@d_c_3
@@d_c_2:	
	mov	[plyrB_cash],ax

@@d_c_3:	
	popad
	ret

ENDP	dec_cash
;}}}


;--------------------------------------------------------------------------
;Increase assets. Amount of assets to increase stored in bx.
;--------------------------------------------------------------------------{{{
PROC	add_assets
	
	pushad
	
	mov	ax, [plyrA_assets]	;Move current player's cash into ax.
	cmp	[curr_plyr], 0
	je	@@a_a_1
	mov	ax, [plyrB_assets]	
@@a_a_1:	
	
	add	ax, bx
	jnc	@@a_a_notcarry
	mov	ax, 0ffffh
					;At this point, ax contains new cash amount.
@@a_a_notcarry:	
	cmp	[curr_plyr], 0		;Update current player's cash.
	jne	@@a_a_2
	mov	[plyrA_assets], ax
	jmp	@@a_a_3
@@a_a_2:	
	mov	[plyrB_assets], ax

@@a_a_3:	
	popad
	ret

ENDP	add_assets
;}}}


;--------------------------------------------------------------------------
; If space is property, check who the owner is before going to sub-action.
;--------------------------------------------------------------------------{{{
PROC	action_property

	;get space owner
	xor	bx, bx
	mov	bl, [curr_space]
	mov	al, [space_owner + bl]
	
	mov	[curr_owner], al		;Updates memory
	
	
	cmp	[curr_owner], 2			;No-one owns the space.
	jne	@@a_p_1
	call	action_property_buy
	jmp	@@a_p_3
	
@@a_p_1:					;If someone owns the space, check who owns the space.
	mov	ah, [curr_plyr]			;Compare current player with current owner.
	cmp	ah, al
	jne	@@a_p_2
	
	call	action_property_upgrade		;Current player owns space.
	jmp	@@a_p_3
	
@@a_p_2:
	call	action_property_rent	;Opponent owns space.
	jmp	@@a_p_3
	
@@a_p_3:	

	ret

ENDP	action_property
;}}}

;--------------------------------------------------------------------------
; Asks if want to buy space with no owner. 
;--------------------------------------------------------------------------{{{
PROC	action_property_buy



	;Ask player if want to buy. Give price.
	
	box	320, 334, 519, 638, black
	cursor	20, 70
	message	buy_msg
	change_color	320, 334, 520, 637, black, blue

	box	352, 366, 519, 638, black
	cursor	22, 70
	message	pass_msg
	change_color	352, 366, 520, 637, black, blue

@@10:
	call	maincontrol
	cmp	[click], 6
	je	@@pass
	cmp	[click], 8
	je	@@buy
	jmp	@@10

@@buy:	
	xor	ax, ax
	xor	bx, bx
	mov	bl, [curr_space]
	mov	al, [buy_price + bl]

		;at this point, ax contains rent price.

	pushad
	mov	bx, ax
	mov	ax, OFFSET buy_end_2
	mov	di, ax
	mov	ax, bx
	call	BinToAsc
	mov	ax, OFFSET buy_msg_2
	mov	di, ax
	call	outmsg
	popad

	mov	bx, ax		;decrease cash
	call	dec_cash
	call	add_assets
	call	update_status

	mov	bl, [curr_space]	;update owner
	mov	al, [curr_plyr]
	mov	[space_owner + bl], al

	xor	bx, bx
	mov	bl, [curr_space]
	add	bx, bx
	mov	ax, [b_row_start + bx]
	mov	[tmp_row_start], ax
	add	ax, 9
	mov	[tmp_row_end], ax
	mov	ax, [p_col_start + bx]
	mov	[tmp_col_start], ax
	add	ax, 16
	mov	[tmp_col_end], ax

	cmp	[curr_plyr], 1
	je	@@20
	box	<[tmp_row_start]>, <[tmp_row_end]>, <[tmp_col_start]>, <[tmp_col_end]>, lightcyan
	jmp	@@pass
@@20:
	box	<[tmp_row_start]>, <[tmp_row_end]>, <[tmp_col_start]>, <[tmp_col_end]>, lightmagenta
	
@@pass:		
	box	320, 334, 519, 638, black
	box	352, 366, 519, 638, black
	ret

ENDP	action_property_buy
;}}}


;--------------------------------------------------------------------------
; Asks if want to upgrade own space (provided not fully upgraded)
;--------------------------------------------------------------------------{{{
PROC	action_property_upgrade

	box	352, 366, 519, 638, black
	cursor	22, 70
	message	pass_msg
	change_color	352, 366, 520, 637, black, blue
	
	mov	bl, [curr_space]
	mov	al, [space_level + bl]
	mov	[curr_level], al
	
	cmp	al, 2		;If hotel, cannot upgrade any more.
	je	@@10

	box	320, 334, 519, 638, black
	cursor	20, 69
	message	upg_msg
	change_color	320, 334, 520, 637, black, blue

@@10:
	call	maincontrol
	cmp	[click], 6
	je	@@pass
	cmp	al, 2		;If hotel, cannot upgrade any more.
	je	@@10
	cmp	[click], 8
	je	@@upg
	jmp	@@10

@@upg:	
	xor	ax, ax
					;BL already contains curr_space.
	mov	al, [upgrade_price + bl]	;Give price.	

	;amt of money put into bx
	mov	bx, ax

	pushad
	mov	bx, ax
	mov	ax, OFFSET upg_end_2
	mov	di, ax
	mov	ax, bx
	call	BinToAsc
	mov	ax, OFFSET upg_msg_2
	mov	di, ax
	call	outmsg
	popad

	call	dec_cash
	call	add_assets
	call	update_status
	;upgrade space
	mov	bl, [curr_space]
	mov	al, [curr_level]
	inc	al
	mov	[space_level + bl], al	
	

	xor	ah, ah
	shl	ax, 5
	mov	dx, ax

	xor	bx, bx
	mov	bl, [curr_space]
	add	bx, bx
	mov	ax, [b_row_start + bx]
	mov	[tmp_row_start], ax
	add	ax, 9
	mov	[tmp_row_end], ax
	mov	ax, [p_col_start + bx]
	mov	[tmp_col_start], ax
	add	ax, dx
	mov	[tmp_col_end], ax

	cmp	[curr_plyr], 1
	je	@@20
	box	<[tmp_row_start]>, <[tmp_row_end]>, <[tmp_col_start]>, <[tmp_col_end]>, lightcyan
	jmp	@@pass
@@20:
	box	<[tmp_row_start]>, <[tmp_row_end]>, <[tmp_col_start]>, <[tmp_col_end]>, lightmagenta
	
@@pass:	
	box	320, 334, 519, 638, black
	box	352, 366, 519, 638, black
	ret

ENDP	action_property_upgrade
;}}}


;--------------------------------------------------------------------------
; Pay rent. No choice! 
;--------------------------------------------------------------------------{{{
PROC	action_property_rent


	call	clear_reg
	
	mov	bl, [curr_space]
	mov	cl, [space_level + bl]
	mov	al, [rent_price + bl]	
				;at this point, ax contains rent price.
	
	cmp	cl,0
	je	@@10
	add	ax,ax
	cmp	cl,1
	je	@@10
	add	ax,ax
@@10:	
	;at this point, ax contains rent price.
	mov	bx, ax
	mov	ax, OFFSET rent_end
	mov	di, ax
	mov	ax, bx
	call	BinToAsc
	mov	ax, OFFSET rent_msg
	mov	di, ax
	call	outmsg
	
	;amt of money put into bx
	call	dec_cash
	;increase opponent's cash
	xor	[curr_plyr],1
	call	add_cash
	xor	[curr_plyr],1

@@pass:
	ret

ENDP	action_property_rent
;}}}


;--------------------------------------------------------------------------
; Jail. 
;--------------------------------------------------------------------------{{{
PROC	action_jail
	pushad	

	xor	bx, bx
	mov	bl, [curr_plyr]
	mov	[plyrA_jailed + bx], 2

	mov	ax, OFFSET jail_msg2
	mov	di, ax
	call	outmsg	

	popad
	ret

ENDP	action_jail
;}}}


;--------------------------------------------------------------------------
; Ends turn.
;--------------------------------------------------------------------------{{{
PROC	end_turn

	mov	bl, [curr_plyr]
	xor	bl, 1
	mov	[curr_plyr], bl
	xor	bh, bh
	
	;If money is zero, end game immediately.
	
	cmp	[plyrA_cash + bx],0
	jne	@@e_t_1
	call	exiter
	
@@e_t_1:	
	;If in jail, decrement.
	
	cmp	[plyrA_jailed + bx], 0
	je	@@e_t_end
	dec	[plyrA_jailed + bx]
@@e_t_end:
	box     456, 480, 0, 640, black
	box	192, 224, 520, 637, black	; erases dice value data on action box
	
	ret

ENDP	end_turn
;}}}


;--------------------------------------------------------------------------
; Exits and prints results
;--------------------------------------------------------------------------{{{
PROC	exiter
	enter	8, 0

	box	96, 384, 160, 480, blue
	vline	100, 380, 164, yellow
	vline	100, 380, 476, yellow
	hline	100, 164, 476, yellow
	hline	380, 164, 477, yellow

	cursor	12, 25
	mov	ax, [plyrA_cash]
	add	ax, [plyrA_assets]
	mov	bx, [plyrB_cash]
	add	bx, [plyrB_assets]
	mov	[word bp - 0], ax
	mov	[word bp - 2], bx

		; Displays summary
		pushad
		xor	eax, eax
		mov	ecx, 1
		cursor	11, 22			; for Player 1
		message	plyr1name		; Outputs Cash
		message	f_cash_msg
		mov	ax, OFFSET outbuff
		mov	di, ax
		mov	ax, [plyrA_cash]
		call	BinToAsc
		message	outbuff

		cursor	12, 22			; Assets
		message	plyr1name
		message	f_asset_msg
		mov	ax, [plyrA_assets]
		call	BinToAsc
		message	outbuff

		cursor	13, 22			; Total
		message	f_total_msg
		mov	ax, [word bp - 0]
		call	BinToAsc
		message	outbuff

		cursor	18, 22			; for Player 2
		message	plyr2name		; outputs cash
		message	f_cash_msg
		mov	ax, OFFSET outbuff
		mov	di, ax
		mov	ax, [plyrB_cash]
		call	BinToAsc
		message	outbuff

		cursor	19, 22			; Assets
		message	plyr2name
		message	f_asset_msg
		mov	ax, [plyrB_assets]
		call	BinToAsc
		message	outbuff

		cursor	20, 22			; Total
		message	f_total_msg
		mov	ax, [word bp - 2]
		call	BinToAsc
		message	outbuff
		popad

	cmp	ax, bx
	ja	@@awin
	je	@@tie
	push	ax
	mov	ax, OFFSET plyr2name
	mov	di, ax
	mov	ax, OFFSET wins_msg
	mov	si, ax
	call	StrConcat
	ctrmsg	plyr2name,7,40
	pop	ax
	jmp	@@Exit
@@awin:
	push	ax
	mov	ax, OFFSET plyr1name
	mov	di, ax
	mov	ax, OFFSET wins_msg
	mov	si, ax
	call	StrConcat
	ctrmsg	plyr1name,7,40
	pop	ax
	jmp	@@Exit

@@tie:
	ctrmsg	tied_msg,7,40

@@Exit:
	change_color	96, 384, 160, 480, black, blue
	change_color	112, 128, 160, 480, lightgray, lightgreen
	mov     ah, 0ch                 ; clear keyboard buffer
        int     21h

	mov	ah, 07h			; wait for ke
	int	21h

	pop	eax
	mov     ah, 00h                 ; set back to original video mode
	int     10h

        mov     ah, 0ch                 ; clear keyboard buffer
        int     21h

	mov	ah, 04ch
	mov	al, [exitStatus]
	int	021h
	
	leave
	ret

ENDP	exiter
;}}}


;--------------------------------------------------------------------------
; Chance.
;--------------------------------------------------------------------------{{{
PROC	action_chance
	pushad
	xor	eax, eax
	call	rand	
	cmp	ax,1
	je	@@c_badluck
	
	;Good luck!
	shl	ax, 4
	mov	bx, ax
	pushad
	mov	ax, OFFSET goodluck_cash
	mov	di, ax
	mov	ax, bx
	call	BinToAsc
	mov	ax, OFFSET goodluck_msg
	mov	di, ax
	call	outmsg
	popad
	call	add_cash
	jmp	@@c_end
@@c_badluck:
	;Bad luck!
	mov	ax, OFFSET badluck_msg
	mov	di, ax
	call	outmsg	
	mov	bx,50
	call	dec_cash
@@c_end:		
	popad
	ret

ENDP	action_chance
;}}}


;--------------------------------------------------------------------------
; Output Message to Status Bar
; Input:
;	di: pointer to start of null terminated char string
;--------------------------------------------------------------------------{{{
PROC	outmsg

        pushad

	box     456, 480, 0, 640, black
	call	StrLength
	mov	ax, cx
	div	[two]
	mov	bx, 39
	sub	bx, ax
	cursor  29, bl
	call	StrWrite

	popad
	ret

ENDP	outmsg
;}}}

;--------------------------------------------------------------------------
; rand process given by lecturer
;--------------------------------------------------------------------------{{{
PROC	rand
	push	bx
	push	cx
	push	dx
	push	es
	
	MOV	ax, 40h
	MOV	es, ax
@@again:
	mov	ax, RTC		;saves current RTC value into ax
	and	al, 07h		;retrieve 0-255
	mov	ah, 00h

	cmp	al, 07h		;if value is 255,roll dice again
	je	@@again

	cmp	al, 00h		;if value is 0, roll dice again
	je	@@again
	
	mov	ah,00		;Now AX contains a single random number.

	pop	es
	pop	dx
	pop	cx
	pop	bx		
	ret
ENDP	rand
;}}}


	END	Start
